package com.hope.hotel.controller;

import com.hope.hotel.common.Result;
import com.hope.hotel.entity.dto.ReviewDTO;
import com.hope.hotel.service.ReviewService;
import com.hope.hotel.utils.AliOSSUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 评论管理控制器
 */
@RestController
@RequestMapping("/review")
@RequiredArgsConstructor
public class ReviewController {

    private final ReviewService reviewService;
    private final AliOSSUtils aliOSSUtils;

    /**
     * 添加评论
     */
    @PostMapping("/add")
    public Result addReview(@RequestBody ReviewDTO reviewDTO, HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        reviewDTO.setUserId(userId);
        return reviewService.addReview(reviewDTO);
    }

    /**
     * 用户获取酒店评论列表
     */
    @GetMapping("/hotel-reviews")
    public Result getHotelReviews(
            @RequestParam String hotelId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        return reviewService.getHotelReviews(hotelId, page, pageSize);
    }

    /**
     * 获取酒店评论统计信息
     */
    @GetMapping("/hotel-stats")
    public Result getHotelReviewStats(@RequestParam String hotelId) {
        return reviewService.getHotelReviewStats(hotelId);
    }

    /**
     * 获取用户评论列表
     */
    @GetMapping("/user-reviews")
    public Result getUserReviews(HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        return reviewService.getUserReviews(userId);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{reviewId}")
    public Result deleteReview(@PathVariable String reviewId, HttpServletRequest request) {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }
        return reviewService.deleteReview(reviewId, userId);
    }

    /**
     * 上传图片
     */
    @PostMapping("/upload-image")
    public Result uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        // 从请求中获取用户ID
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            // 使用阿里云OSS上传图片
            String url = aliOSSUtils.upload(file);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片上传失败: " + e.getMessage());
        }
    }

    /**
     * 管理员获取酒店评论列表（带筛选条件）
     */
    @GetMapping("/admin/hotel-reviews")
    public Result getHotelReviewsForAdmin(
            @RequestParam String hotelId,
            @RequestParam(required = false) Integer rating,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        // 从请求中获取酒店管理员信息
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        
        // 转换status字符串为数值
        Integer statusCode = null;
        if (status != null && !status.isEmpty()) {
            try {
                // 使用ReviewVO中的静态方法转换
                statusCode = com.hope.hotel.entity.vo.ReviewVO.getStatusCode(status);
            } catch (Exception e) {
                return Result.error("状态参数无效");
            }
        }
        
        return reviewService.getHotelReviewsForAdmin(hotelId, adminId, rating, statusCode, startDate, endDate, page, pageSize);
    }
    
    /**
     * 管理员回复评论
     */
    @PostMapping("/admin/reply")
    public Result replyReview(
            @RequestParam String reviewId,
            @RequestParam String reply,
            HttpServletRequest request) {
        // 从请求中获取酒店管理员信息
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        
        return reviewService.replyReview(reviewId, adminId, reply);
    }
    
    /**
     * 管理员更新评论状态（隐藏/显示）
     */
    @PostMapping("/admin/update-status")
    public Result updateReviewStatus(
            @RequestParam String reviewId,
            @RequestParam String status,
            HttpServletRequest request) {
        // 从请求中获取酒店管理员信息
        String adminId = (String) request.getAttribute("adminId");
        if (adminId == null) {
            return Result.error("管理员未登录");
        }
        
        // 转换status字符串为数值
        Integer statusCode;
        try {
            statusCode = com.hope.hotel.entity.vo.ReviewVO.getStatusCode(status);
        } catch (Exception e) {
            return Result.error("状态参数无效");
        }
        
        return reviewService.updateReviewStatus(reviewId, adminId, statusCode);
    }
} 